home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8269 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  76 lines

  1. Path: news.vcnet.com!usenet
  2. From: swedecj@vcnet.com (Carl Jacobson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: hex to dec function?
  5. Date: Sat, 02 Mar 1996 20:30:22 GMT
  6. Organization: Internet Access of Ventura County 805.383.3500
  7. Message-ID: <4hab7t$uo4@news.vcnet.com>
  8. References: <4gdh1b$bg@mailhost.mwmicro.com> <4gdolr$ha9@madeline.INS.CWRU.Edu> <4gg3h0INN59e@keats.ugrad.cs.ubc.ca> <danpop.824997504@rscernix>
  9. NNTP-Posting-Host: port12.vcnet.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. danpop@mail.cern.ch (Dan Pop) wrote:
  13.  
  14. >In <4gg3h0INN59e@keats.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  15.  
  16. >>In article <4gdolr$ha9@madeline.INS.CWRU.Edu>,
  17. >>Michael A. Balfour <mab22@po.CWRU.Edu> wrote:
  18. >> >In a previous article, aschlies@citynet.net () says:
  19. >> >>Hi There,
  20. >> >>Is there a function that converts HEX to Dec in ANSI C?
  21. >> >You could try sscanf.  For example:
  22. >> >main()
  23. >> >{
  24. >> >  char hex[9];
  25. >> >  int dec;
  26. >> >  strcpy(hex,"FFFF");
  27. >>
  28. >>Of course, someone will change the above to "         FFFFF", thus clobbering
  29. >>your activation record, since you comitted the hex array to hold only 9
  30. >>characters. 
  31. >>
  32. >>There is no reason why you can't use   char *hex = "FFFF";  in such a small
  33. >>example.
  34.  
  35. >For such a small example, sscanf("FFFF", "%x", &dec); would have done the
  36. >job :-)
  37.  
  38. >Note, however, that there are broken implementations where only the original
  39. >example would work, while Kazimir's and mine would segfault!  This
  40. >is because string literals _may_ be stored in read-only memory segments
  41. >and some old sscanf implementations attempted to write back (actually
  42. >to ungetc) the first character that couldn't be converted (the terminating
  43. >null in this case).  A few years ago, somebody actually posted a similar
  44. >example which crashed on his system and he couldn't understand why.
  45.  
  46. >Dan
  47. >--
  48. >Dan Pop
  49. >CERN, CN Division
  50. >Email: danpop@mail.cern.ch 
  51. >Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  52.  
  53. Why does the following hang my pc when the printf statement is being
  54. executed. If I put a breakpoint on the printf, watch and reset,
  55. everything seems OK up to that point. The answer appears to be in the
  56. above comments, but I do not understand the premise.
  57.  
  58.    char *pref = "0x";
  59.    char *suff;
  60.    long numb;
  61.  
  62.       scanf("%s",suff);
  63.       strcat(pref,suff);
  64.       sscanf(pref,"0x%lx",&numb);
  65.       
  66.       printf("  %ld\n",numb);
  67.  
  68. TIA
  69. Carl
  70. ---------------------------------------------------------------
  71. Carl Jacobson                          swedecj@vcnet.com
  72. P.O. Box 3607                          Phone: US 805.523.1724
  73. Thousand Oaks, CA 91359                Fax  : US 805.523.1454
  74.  
  75.  
  76.